﻿namespace Reals.Existence
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using Reals.Parellel;

  /// <summary>
  /// 微存在.
  /// </summary>
  /// <remarks>
  /// "存在"本身并不需要存在.
  /// 大量的,不同层次上的微过程
  /// 交互作用,并行延续,循环往复,
  /// 这些事实的表象也就是存在.
  /// 过程并不等价为时间,所以
  /// 存在也并不等价为空间.
  /// 我们所认识的物体(object)
  /// 在本质上都基于时空统一体, 
  /// 是时空统一体的表现.
  /// 但是,如此不能拆分的世界,
  /// 将使得我们的认识和对特定
  /// 事物的操作困难重重,所以,
  /// 在系统的实现上,我们仍然
  /// 将事(过程)和物(对象)相分离.
  /// 不过,这种分离不是绝对的,
  /// 现实世界的真实情况是,
  /// 持久的(或者是多变的)存在
  /// 对应微过程的循环往复.
  /// 由此,我们定义微存在为
  /// 微过程的循环往复.
  /// 微存在不一定是微小的,
  /// 任何规模的存在都可以
  /// 由此实现.
  /// 在实现上,我们定义
  /// 微存在为一组相关于特定
  /// "物体"的微过程集合.
  /// </remarks>
  public interface MicroExistent:IDisposable
  {
    /// <summary>
    /// 获取置于微存在底层的微循环.
    /// </summary>
    MicroCycling Cycling { get;}
    /// <summary>
    /// 判别当前微存在是否已经附着.
    /// </summary>
    bool HasAttached { get;}
    /// <summary>
    /// 附着到指定循环.
    /// </summary>
    /// <remarks>
    /// 附着之后,可以将相关于当前微存在的微过程添加到
    /// 微循环中.
    /// </remarks>
    bool Attach(MicroCycling cycling);
    /// <summary>
    /// 从微循环中分离.
    /// </summary>
    /// <remarks>
    /// 基类的缺省实现应
    /// 完成从给定微循环
    /// 中移除所有支持当
    /// 前微存在的微过程.
    /// </remarks>
    void Detach();
    /// <summary>
    /// 判别当前对象是否已经被处置。
    /// </summary>
    bool IsDisposed { get;}
  }
  /// <summary>
  /// 微存在的实现.
  /// </summary>
  public class MicroExistentImpl : MicroExistent
  {
    #region Shortcuts
    public static MicroExistent of(MicroCycling cycling)
    {
      return new MicroExistentImpl(cycling);
    }
    #endregion
    #region Fields
    protected MicroCycling cycling = null;
    protected bool isDisposed = false;
    #endregion
    #region Properties
    public virtual MicroCycling Cycling
    {
      get { return this.cycling; }
    }
    public virtual bool HasAttached
    {
      get { return this.cycling != null; }
    }
    public virtual bool IsDisposed
    {
      get
      {
        return this.isDisposed;
      }
    }
    #endregion
    #region Constructors
    public MicroExistentImpl() : this(null) { }
    public MicroExistentImpl(MicroCycling cycling)
    {
      this.Attach(cycling);
    }
    ~MicroExistentImpl()
    {
      this.Dispose();
    }
    #endregion
    #region Methods
    /// <summary>
    /// 附着的缺省实现.
    /// </summary>
    public virtual bool Attach(MicroCycling cycling)
    {
      if (cycling != null)
      {
        return this.OnAttachCyclingTo(cycling);
      }
      return false;
    }
    /// <summary>
    /// 脱离的缺省实现.
    /// </summary>
    /// <remarks>
    /// 实现将忽略以脱离的情况.
    /// </remarks>
    public virtual void Detach()
    {
      if (this.cycling != null)
      {
        this.OnDetachCyclingFrom(this.cycling);
        this.cycling = null;
      }
    }
    /// <summary>
    /// 附着微循环的内部扩展.
    /// </summary>
    /// <remarks>
    /// 请在派生类先调用基类的实现.
    /// </remarks>
    protected virtual bool OnAttachCyclingTo(MicroCycling cycling)
    {
      if (this.cycling != null)
      {
        this.Detach();
      }
      return (this.cycling = cycling) != null;
    }
    /// <summary>
    /// 脱离微循环的内部扩展.
    /// </summary>
    /// <remarks>
    /// 请在派生类先调用基类的实现.
    /// </remarks>
    protected virtual void OnDetachCyclingFrom(MicroCycling cycling)
    {
      if (cycling != null)
      {
        cycling.RemoveTarget(this);
      }
    }
    #endregion

    #region IDisposable

    public virtual void Dispose()
    {
      if (!this.IsDisposed)
      {
        GC.SuppressFinalize(this);
        this.Dispose(true);
      }
    }

    protected virtual void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (this.cycling != null)
        {
          this.cycling.Dispose();
          this.cycling = null;
        }
      }
      this.isDisposed = true;
    }

    #endregion
  }
}
